package com.weharvest.edu.service.Impl;

import com.weharvest.edu.dao.BackstageUserDao;
import com.weharvest.edu.dao.mapper.BackstageUserDaoMapper;
import com.weharvest.edu.dto.BackstageLoginDTO;
import com.weharvest.edu.entity.BackstageMenu;
import com.weharvest.edu.entity.BackstageUser;
import com.weharvest.edu.enums.CommonEnums;
import com.weharvest.edu.enums.ResultEnum;
import com.weharvest.edu.exception.EduException;
import com.weharvest.edu.service.EduService;
import com.weharvest.edu.token.Jwt;
import com.weharvest.edu.utlis.KeyUtil;
import com.weharvest.edu.utlis.SessionUtil;
import com.weharvest.edu.view.BackstageUserDetailView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class EduServiceImpl implements EduService {


    @Resource
    private BackstageUserDao dao;
    @Resource
    private BackstageUserDaoMapper daoMapper;

    @Override
    public String login(BackstageLoginDTO dto, HttpServletRequest request) {
        //验证密码是否正确
        BackstageUser backstageUser = dao.findByNameAndPasswordAndStateAndFlag(
                dto.getUserName(),
                KeyUtil.getEncryptionPassWord(dto.getPassword()),
                CommonEnums.state.STATE_AVAILABLE.getCode(),
                CommonEnums.flag.FLAG_AVAILABLE.getCode());
        // 密码错误
        if (backstageUser == null) {
            return null;
        } else {
            //生成token
            Map<String, Object> payload = new HashMap<>();
            payload.put("uid", backstageUser.getId());
            Date date = new Date();
            long ext = 60 * 60 * 12 * 1000;
            payload.put("ext", ext);
            payload.put("iat", date.getTime());
            String token = Jwt.createToken(payload);
            if (token.isEmpty() || token == null) {
                throw new EduException(ResultEnum.LOGIN_ERROR);
            }
            //录入session
            HttpSession session = request.getSession();
            session.setAttribute("uid", backstageUser.getId());
            session.setAttribute("organizationId", backstageUser.getOrganizationId());
            return token;
        }

    }

    @Override
    public BackstageUserDetailView detail(HttpServletRequest request, HttpServletResponse response) {
        //获取token
        String uid = SessionUtil.getSession(request, "uid");
        if (StringUtils.isEmpty(uid)) {
            throw new EduException(ResultEnum.SESSION_UID_ERROR);
        }
        //获取当前用户
        BackstageUserDetailView view = daoMapper.queryByBackstageUserDetail(uid);
        if (null == view) {
            throw new EduException(ResultEnum.DETAIL_UID_ERROR);
        }
        return view;
    }

    @Override
    public List<BackstageMenu> findMenus(String uid) {
        if (null == uid || StringUtils.isEmpty(uid)) {
            log.error("[拦截器]传入uid不存在");
            throw new EduException(ResultEnum.EDU_MENUS_FIND_INPUT_NOT_PARAMS);
        }
        return null;
    }


}

